﻿using CleanToolkit.Comparison.DiffModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Globalization;

namespace CleanToolkit.Console
{
    class Program
    {
        public const string cst_SchemaPath = @"G:\1 - Projets\i-BP\EspaceCo\EpurationBao\Lists\Actions\Schema.xml";
        public const string cst_FieldsPath = @"D:\0 - Projets\EspaceCollab\Sources\Trunk\Espaces Collaboratifs\Composants\iBP.Bao\Champs\FdiBP\Elements.xml";
        public const string cst_DiffsPath = @"G:\1 - Projets\i-BP\EspaceCo\EpurationBao\Diffs.xml";
        public const string cst_FieldsIDsPath = @"D:\Tools\Data\Probleme\Schema.xml";
        public const string cst_ExportCTPath = @"D:\Tools\Data\CTExtracted.xml";
        public const string cst_ExportSchemaPath = @"D:\Tools\Data\SchemaExtracted.xml";

        public const string cst_fieldsdefPath = @"D:\0 - Projets\EspaceCollab\Sources\Trunk\Espaces Collaboratifs\Composants\iBP.Bao\Champs\FdiBP\Elements.old.xml";
        public const string cst_fieldsCleanedPath = @"D:\0 - Projets\EspaceCollab\Sources\Trunk\Espaces Collaboratifs\Composants\iBP.Bao\Champs\FdiBP\ElementsWithoutRes.xml";
        public const string cst_fieldsResPath = @"D:\0 - Projets\EspaceCollab\Sources\Trunk\Espaces Collaboratifs\Composants\iBP.Bao\Resources\iBPBao.resx";

        static void Main(string[] args)
        {
            //GetDiffs();

            GetSchemaFields();

            //CleanFieldsDef();

            System.Console.ReadKey();
        }

        private static void GetCTFields()
        {
            XDocument xDoc = CleanerFacade.GetContentTypeFieldsDef(cst_FieldsPath, cst_FieldsIDsPath);
            xDoc.Save(cst_ExportCTPath);
        }

        private static void GetSchemaFields()
        {
            //XDocument xDoc = CleanerFacade.GetSchemaFieldsDef(cst_FieldsPath, cst_FieldsIDsPath);
            XDocument xDoc = CleanerFacade.GetSchemaFieldsDef(
                @"D:\0 - Projets\EspaceCollab\Sources\Trunk\Espaces Collaboratifs\Composants\iBP.Bao\Champs\FdiBP\Elements.xml",
                @"D:\0 - Projets\EspaceCollab\Sources\Trunk\Espaces Collaboratifs\Composants\iBP.Bao\ContentTypesList\CTAutreRessource\Elements.xml"
                );
            xDoc.Save(cst_ExportSchemaPath);
        }

        private static void GetDiffs()
        {
            SchemaFieldsDiff diffs = CleanerFacade.GetFieldsDiff(cst_SchemaPath, cst_FieldsPath);
            XDocument xDiffs = diffs.Serialize();

            System.Console.WriteLine(xDiffs.ToString());

            xDiffs.Save(cst_DiffsPath);
        }

        private static void CleanFieldsDef()
        {
            XDocument fields = CleanerFacade.RemoveResFromFieldsStaticName(cst_fieldsdefPath, cst_fieldsResPath);
            fields.Save(cst_fieldsCleanedPath);
        }

        private static void CreateResNames()
        {
            XDocument xd = XDocument.Load(@"d:\CHOICE.xml");
            XDocument xdResx = new XDocument();
            XElement xResRoot = new XElement("DATAS");

            int index = 1;
            foreach (XElement xe in xd.Descendants("CHOICE"))
            {
                string buffer = xe.Value;
                buffer = buffer
                    .Replace("é", "e")
                    .Replace("(", "")
                    .Replace(")", "")
                    .Replace(".", "")
                    .Replace("'", "")
                    .Replace("-", "");

                buffer = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(buffer)
                    .Replace(" ", "");

                buffer = string.Format("iBPRoleProjet_{0}_{1}", index.ToString("00"), buffer);
                XElement xBufferRes = new XElement("data");
                xBufferRes.SetAttributeValue("name", buffer);
                xBufferRes.SetAttributeValue("space", "preserve");
                xBufferRes.Add(new XElement("value", xe.Value));
                xResRoot.Add(xBufferRes);

                buffer = string.Format("$Resources:iBO.BaoChoices,{0};", buffer);
                xe.SetValue(buffer);

                index++;

                System.Console.WriteLine(buffer);
            }

            xResRoot.Save(@"d:\CHOICES_Resx.xml");
            xd.Save(@"d:\CHOICES_Field.xml");

        }
    }
}
